package com.qyhl.webtv.module_news.news.information.info.fragment;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.AdvHomeBean;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by qyhl on 2017/11/28.
 */

public class InformationFragmentModel implements InformationFragmentContract.InformationFragmentModel {
    private InformationFragmentPresenter mPresenter;

    public InformationFragmentModel(InformationFragmentPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取新闻数据
     *
     * @param sectionId
     * @param newsId
     */
    @Override
    public void getDatas(String sectionId, final String newsId) {

        EasyHttp.get(NewsUrl.GET_HOME_NEWS)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("InnerCode", sectionId)
                .params("ID", newsId)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        //刷新
                        if ("0".equals(newsId) || "1".equals(newsId)) {
                            if (e.getCode() == 202) {
                                mPresenter.setError(2, "暂无内容！");
                            } else {
                                mPresenter.setError(3, "解析出错，加载失败！");
                            }
                        } else { //加载
                            if (e.getCode() == 202) {
                                mPresenter.setError(4, "no more");
                            } else {
                                mPresenter.setError(5, "解析出错，加载失败！");
                            }
                        }
                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        if ("0".equals(newsId) || "1".equals(newsId)) {//刷新
                                if (newsBeans != null && newsBeans.size() > 0) {
                                    mPresenter.setDatas(newsBeans, false);
                                } else {
                                    mPresenter.setError(2, "暂无内容！");
                                }
                        } else { //加载
                            if (newsBeans != null && newsBeans.size() > 0) {
                                mPresenter.setDatas(newsBeans, true);
                            } else {
                                mPresenter.setError(4, "no more");
                            }
                        }
                    }
                });


    }

    /**
     * 获取置顶数
     *
     * @param sectionId
     */
    @Override
    public void getHeaderNum(String sectionId) {
        String url = CommonUtils.getInstance().getCmsUrl();

        EasyHttp.get(url)
                .params("method", "queryTopArticle")
                .params("siteId", CommonUtils.getInstance().getSiteName() + "")
                .params("catalogID", sectionId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setHeaderError();
                    }

                    @Override
                    public void onSuccess(String s) {
                        try {
                            JSONObject object = new JSONObject(s);
                            int num = Integer.valueOf(object.optString("returnMeg"));
                            mPresenter.setHeaderNum(num);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            mPresenter.setHeaderError();
                        }
                    }
                });


    }

    /**
     * 获取广告
     *
     * @param sectionId
     */
    @Override
    public void getAdvData(String sectionId) {

        EasyHttp.get(NewsUrl.GET_LIST_ADV)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("catalogId", sectionId)
                .execute(new SimpleCallBack<List<AdvHomeBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setAdvError("广告数据解析失败！");
                    }

                    @Override
                    public void onSuccess(List<AdvHomeBean> advHomeBeans) {
                        if (advHomeBeans != null && advHomeBeans.size() > 0) {
                            mPresenter.setAdvData(advHomeBeans);
                        } else {
                            mPresenter.setAdvError("无广告！");
                        }
                    }
                });

    }
}
